package org.xxpay.pay.channel.ywxpay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.common.util.XXPayUtil;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.core.entity.WxUser;
import org.xxpay.pay.channel.BasePayment;
import org.xxpay.pay.mq.BaseNotify4MchPay;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: dingzhiwei
 * @date: 19/1/22
 * @description: 云微信支付接口
 */
@Service
public class YwxpayService extends BasePayment {

    @Autowired
    public BaseNotify4MchPay baseNotify4MchPay;

    private static final MyLog _log = MyLog.getLog(YwxpayService.class);

    @Override
    public String getChannelName() {
        return YwxpayConfig.CHANNEL_NAME;
    }


    /**
     * 调用微信收款二维码接口
     * @return
     */
    public JSONObject getPaymentQrcode(String url, WxUser wxUser, PayOrder payOrder) {
        String paymentUrl = url + "/wxapi/payment_qrcode";
        Map paramMap = new HashMap();
        paramMap.put("randomId", wxUser.getRandomId());
        paramMap.put("amount", payOrder.getAmount());
        paramMap.put("orderId", payOrder.getPayOrderId());
        paymentUrl += "?" + XXPayUtil.genUrlParams(paramMap);
        _log.info("获取微信收款二维码,url={}", paymentUrl);
        String result = XXPayUtil.call4Post(paymentUrl);
        if(StringUtils.isEmpty(result)) return null;
        return JSON.parseObject(result);
    }

    /**
     * 查询微信登录状态
     * @return
     */
    public JSONObject getLoginStatus(String url, WxUser wxUser) {
        String loginUrl = url + "/wxapi/login_status";
        Map paramMap = new HashMap();
        paramMap.put("randomId", wxUser.getRandomId());
        loginUrl += "?" + XXPayUtil.genUrlParams(paramMap);
        _log.info("获取微信登录状态,url={}", loginUrl);
        String result = XXPayUtil.call4Post(loginUrl);
        if(StringUtils.isEmpty(result)) return null;
        return JSON.parseObject(result);
    }

}
